//
//  TradingRecordVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/3.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class TradingRecordVc: TTBaseVC {
    
    let BuyCellID = "BuyCell"
    let RechargeCellID = "RechargeCell"
    let WithdrawCellID = "WithdrawCell"
    let TBCellID = "TBCell"
    var startPage:Int = 1
    var totalPages:Int = 1
    
    lazy var headview:UIView = {
        let view = UIView.init(frame: FRAME_CGFloat(0, 0, SCREEN_WIDTH, 25))
        view.backgroundColor = RGB(230, 67, 64, 0.1)
        let left = UILabel.init(frame: FRAME_CGFloat(27.5, 5, 50, 15))
        left.text = "交易类目"
        left.font = FONT(11)
        view.addSubview(left)
        let centre = UILabel.init(frame: FRAME_CGFloat(109.5, 5, 50, 15))
        centre.text = "交易详情"
        centre.font = FONT(11)
        view.addSubview(centre)
        let bottom = UILabel.init(frame: FRAME_CGFloat(SCREEN_WIDTH - 71.5, 5, 50, 15))
        bottom.text = "交易状态"
        bottom.font = FONT(11)
        view.addSubview(bottom)
        return view
    }()
    
    lazy var tableView: TTTableView = {
        let tableView = TTTableView.init(frame: FRAME_CGFloat(0, 25, SCREEN_WIDTH, SCREEN_HEIGHT - OFFSET_TOP - 25), style: UITableView.Style.plain)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = UIColor.white
        tableView.bounces = true
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(NIBNAME(BuyCellID), forCellReuseIdentifier: BuyCellID)
        tableView.register(NIBNAME(RechargeCellID), forCellReuseIdentifier: RechargeCellID)
        tableView.register(NIBNAME(WithdrawCellID), forCellReuseIdentifier: WithdrawCellID)
        tableView.register(NIBNAME(TBCellID), forCellReuseIdentifier: TBCellID)
        tableView.tableFooterView = UIView.init(frame: .zero)
        return tableView
    }()
    
    var listModel:[PkgRecordModel] = [PkgRecordModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "交易记录"
        
        setUI()
        setData()
        
        self.tableView.initMjFooterWithOffest(0) { (taleView) in
            self.startPage += 1
            self.setData()
        }
    }

    func setUI() {
        self.view.addSubview(self.headview)
        self.view.addSubview(self.tableView)
        
    }
    
    func setData() {
        if self.startPage > self.totalPages {
            self.tableView.endAllRefreshing()
            return
        }
        MTProgressHUD.show()
        MineRequestModel.loadPkgRecord(parameters:["startPoint": self.startPage,"maxPage": 20],programId: [String.getUserId(),0]) { (success, message, model, page) in
            self.totalPages = page ?? 1
            if success && model != nil {
                for dic in model! {
                    self.listModel.append(dic)
                }
                self.tableView.reloadData()
            } else {
                TTAlertView.showMessage(message: message)
            }
            if self.listModel.count == 0 || self.listModel == nil {
                self.tableView.setNoData(nodata: true)
            } else {
                self.tableView.setNoData(nodata: false)
            }
            MTProgressHUD.hidded(true)
        }
        self.tableView.endAllRefreshing()
    }
}

// MARK: *******  协议  ********
extension TradingRecordVc: UITableViewDelegate,UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.listModel.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        switch self.listModel[indexPath.row].payStatus {
        case 0:
            return 53.5
        case 1:
            return 113.5
        case 2:
            return 53.5
        case 3:
            return 72
        case 10:
            return 72
        case 11:
            return 72
        case 12:
            return 72
        default:
            return 0
        }

    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if self.listModel[indexPath.row].payStatus == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: BuyCellID) as! BuyCell
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = self.listModel[indexPath.row].title
            cell.label3.text = "\(String.money(self.listModel[indexPath.row].everyPrice ) ?? "--")听币"
            cell.label4.text = "\(self.listModel[indexPath.row].amount )件"
//            cell.label5.text = totalPrice(amount: self.listModel?[indexPath.row].amount, everyPrice: self.listModel?[indexPath.row].everyPrice)
            cell.label5.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else if self.listModel[indexPath.row].payStatus == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: RechargeCellID) as! RechargeCell
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else if self.listModel[indexPath.row].payStatus == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: WithdrawCellID) as! WithdrawCell
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else if self.listModel[indexPath.row].payStatus == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: TBCellID) as! TBCell
            cell.leftLabel.text = "签到有礼"
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = "签到奖励"
            cell.label3.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else if self.listModel[indexPath.row].payStatus == 12 {
            let cell = tableView.dequeueReusableCell(withIdentifier: TBCellID) as! TBCell
            cell.leftLabel.text = "小姐姐送福利"
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = "小姐姐送听币"
            cell.label3.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else if self.listModel[indexPath.row].payStatus == 10 || self.listModel[indexPath.row].payStatus == 11 {
            let cell = tableView.dequeueReusableCell(withIdentifier: TBCellID) as! TBCell
            cell.leftLabel.text = "关注有礼"
            cell.label1.text = self.listModel[indexPath.row].createTimeStr
            cell.label2.text = "关注奖励"
            cell.label3.text = "\(String.money(self.listModel[indexPath.row].balance ) ?? "--")听币"
            cell.selectionStyle = .none
            return cell
        } else {
            return UITableViewCell.init()
        }
        
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    
    func totalPrice(amount:Int?,everyPrice:NSNumber?) -> String {
        let total = Float(amount ?? 0) * Float(truncating: everyPrice ?? 0.0)
        return "\(total)"
    }
    
}
